package com.yonyou.brigade.third_parties.meiya.util;

import com.xiaoleilu.hutool.util.IdcardUtil;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.third_parties.meiya.model.MeiyaUser;
import com.yonyou.brigade.third_parties.meiya.model.third.ApplyPassengerInfo;
import com.yonyou.ybzboot.uc.dto.UcUserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户转换工具类
 *
 * @author kangfw5
 * @since 2019-06-13
 */
public class MeiyaUserConvertUtil {

    private static Logger logger= LoggerFactory.getLogger(MeiyaUserConvertUtil.class);


    public static MeiyaUser ucUser2Meiya(String tenantId, String companyId, UcUserDto ucUserDto) {
        MeiyaUser meiyaUser = new MeiyaUser();
        meiyaUser.setCertNumber(ucUserDto.getPassengerId());
        meiyaUser.setCertType("身份证");
        meiyaUser.setCnName(ucUserDto.getUserName());
        meiyaUser.setEmail(ucUserDto.getEmail());
        meiyaUser.setCompanyId(companyId);
        meiyaUser.setCreateTime(new Date());
        meiyaUser.setMobile(ucUserDto.getPhone());
        meiyaUser.setOrg(ucUserDto.getOrgPk());
        meiyaUser.setOutsidePassengerId(ucUserDto.getPersonCode());
        meiyaUser.setPassengerName(ucUserDto.getUserName());
        meiyaUser.setPassengerType("成人");
        meiyaUser.setStaffId(ucUserDto.getPersonCode());
        meiyaUser.setStatus(ucUserDto.getValid());
        meiyaUser.setTenant(tenantId);
        meiyaUser.setUserId(ucUserDto.getUserId());
        meiyaUser.setdName(ucUserDto.getOrgName() + "/" + ucUserDto.getDeptName());
        meiyaUser.setUserName(ucUserDto.getPersonCode());
        meiyaUser.setHolderId(ucUserDto.getPostName());
        return meiyaUser;
    }

    public static List<MeiyaUser> ucUser2Meiya(String tenantId, String companyId, List<UcUserDto> ucUserList) {
        List<MeiyaUser> meiyaUserList = new ArrayList<>();
        for (UcUserDto ucUserDto : ucUserList) {
            meiyaUserList.add(ucUser2Meiya(tenantId, companyId, ucUserDto));
        }
        return meiyaUserList;
    }

    public static ApplyPassengerInfo ucUser2ApplyPsg(String tenantId, String companyId, UcUserDto ucUserDto, BriChannel briChannel) {
        ApplyPassengerInfo applyPassenger = new ApplyPassengerInfo();
        applyPassenger.setCostCenterID(ucUserDto.getOrgName());
        applyPassenger.setStaffCode(ucUserDto.getPersonCode());
        applyPassenger.setCnName(ucUserDto.getUserName());
        applyPassenger.setCompanyId(companyId);
        applyPassenger.setDName(ucUserDto.getOrgName() + "/" + ucUserDto.getDeptName());
        applyPassenger.setMobile(ucUserDto.getPhone());
        applyPassenger.setPassengerType("成人");
        applyPassenger.setEmail(ucUserDto.getEmail());
        if (BriChannel.SYNCRANK == briChannel.getSyncRank()){
            applyPassenger.setHolderId(ucUserDto.getPostName());
        }
        try{
            String gender = IdcardUtil.getGenderByIdCard(ucUserDto.getPassengerId()) == 0 ? "女":"男";
            applyPassenger.setSex(gender);
        }catch (Exception e){
            applyPassenger.setSex("女");
            logger.error("根据身份证获取性别异常，身份证号：" + ucUserDto.getPassengerId() );
        }
        return applyPassenger;
    }


}
